Home:ALL Converter>Normal mapping working incorrectly, weird half-light effect

Normal mapping working incorrectly, weird half-light effect

Ask Time:2020-02-24T01:17:04         Author:vosure

Json Formatter

We are trying to implement normal mapping in our 2D Game Engine and get weird effect. If normal is set manually like that vec3 Normal = vec3(0.0, 0.0, 1.0) light works correctly, but we dont get "deep" effect that we want to achieve by normal mapping: enter image description here But if we get normal using normal map texture: vec3 Normal = texture(NormalMap, TexCoord).rgb it doesn't work at all. What should not be illuminated is illuminated and vice versa (such as the gaps between the bricks). And besides this, a dark area is on the bottom (or top, depending on the position of the light) side of the texture. enter image description here Although the texture of the normal map itself looks fine: enter image description here This is our fragment shader:

#version 330 core
layout (location = 0) out vec4 FragColor;

in vec2 TexCoord;
in vec2 FragPos;

uniform sampler2D OurTexture;
uniform sampler2D NormalMap;

struct point_light
{
    vec3 Position;
    vec3 Color;
};

uniform point_light Light;

void main()
{
    vec4 Color = texture(OurTexture, TexCoord);
    vec3 Normal = texture(NormalMap, TexCoord).rgb;

    if (Color.a < 0.1)
        discard;

    vec3 LightDir = vec3(Light.Position.xy - FragPos, Light.Position.z);

    float D = length(LightDir);

    vec3 L = normalize(LightDir);
    Normal = normalize(Normal * 2.0 - 1.0);

    vec3 Diffuse = Light.Color * max(dot(Normal, L), 0);
    vec3 Ambient = vec3(0.3, 0.3, 0.3);

    vec3 Falloff = vec3(1, 0, 0);

    float Attenuation = 1.0 /(Falloff.x + Falloff.y*D + Falloff.z*D*D);
    vec3 Intensity = (Ambient + Diffuse) * Attenuation;

    FragColor = Color * vec4(Intensity, 1);
}

And vertex as well:

#version 330 core
layout (location = 0) in vec2 aPosition;
layout (location = 1) in vec2 aTexCoord;

uniform mat4 Transform;
uniform mat4 ViewProjection;

out vec2 FragPos;
out vec2 TexCoord;

void main()
{
    gl_Position = ViewProjection * Transform * vec4(aPosition, 0.0, 1.0);
    TexCoord = aTexCoord;
    FragPos = vec2(Transform * vec4(aPosition, 0.0, 1.0));
}

I google about that and found some people that get the same result, but their questions remained unanswered. Any idea of what is the cause?

Author:vosure,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60364788/normal-mapping-working-incorrectly-weird-half-light-effect
yy